using System;
using System.Drawing;
using EPiServer.Common;
using EPiServer.Common.Publishing;
using EPiServer.Common.Security;
using EPiServer.Community.ImageGallery;
using EPiServer.Community.ImageGallery.Queries;
using Image=EPiServer.Community.ImageGallery.Image;

namespace EPiAbstractions.Community.ImageGallery
{
    public class ImageGalleryHandlerFacade : IImageGalleryHandlerFacade
    {
        private static ImageGalleryHandlerFacade _instance;

        public ImageGalleryHandlerFacade()
        {
            ImageGalleryHandler.ImageAdded += OnImageAdded;
            ImageGalleryHandler.ImageUpdated += OnImageUpdated;
            ImageGalleryHandler.ImageRemoved += OnImageRemoved;
            ImageGalleryHandler.ImageVote += OnImageVote;
            ImageGalleryHandler.ImageGalleryAdded += OnImageGalleryAdded;
            ImageGalleryHandler.ImageGalleryUpdated += OnImageGalleryUpdated;
            ImageGalleryHandler.ImageGalleryRemoved += OnImageGalleryRemoved;
            ImageGalleryHandler.ImageCommentAdded += OnImageCommentAdded;
            ImageGalleryHandler.ImageCommentUpdated += OnImageCommentUpdated;
            ImageGalleryHandler.ImageCommentRemoved += OnImageCommentRemoved;
        }

        public static ImageGalleryHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ImageGalleryHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IImageGalleryHandlerFacade Members

        public event EPiServerCommonEventHandler ImageAdded;
        public event EPiServerCommonEventHandler ImageUpdated;
        public event EPiServerCommonEventHandler ImageRemoved;
        public event EPiServerCommonEventHandler ImageVote;
        public event EPiServerCommonEventHandler ImageGalleryAdded;
        public event EPiServerCommonEventHandler ImageGalleryUpdated;
        public event EPiServerCommonEventHandler ImageGalleryRemoved;
        public event EPiServerCommonEventHandler ImageCommentAdded;
        public event EPiServerCommonEventHandler ImageCommentUpdated;
        public event EPiServerCommonEventHandler ImageCommentRemoved;

        public virtual EPiServer.Community.ImageGallery.ImageGallery AddImageGallery(EPiServer.Community.ImageGallery.ImageGallery gallery)
        {
            return ImageGalleryHandler.AddImageGallery(gallery);
        }

        public virtual void UpdateImageGallery(EPiServer.Community.ImageGallery.ImageGallery gallery)
        {
            ImageGalleryHandler.UpdateImageGallery(gallery);
        }

        public virtual void RemoveImageGallery(EPiServer.Community.ImageGallery.ImageGallery gallery)
        {
            ImageGalleryHandler.RemoveImageGallery(gallery);
        }

        public virtual EPiServer.Community.ImageGallery.ImageGallery GetImageGallery(Int32 id)
        {
            return ImageGalleryHandler.GetImageGallery(id);
        }

        public virtual ImageGalleryCollection GetImageGalleryChildren(EPiServer.Community.ImageGallery.ImageGallery imageGallery)
        {
            return ImageGalleryHandler.GetImageGalleryChildren(imageGallery);
        }

        public virtual ImageGalleryCollection GetImageGalleries(Int32 page, Int32 pageSize, ImageGallerySortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImageGalleries(page, pageSize, sortOrder);
        }

        public virtual ImageGalleryCollection GetImageGalleries(Int32 page, Int32 pageSize, out Int32 totalItems,
                                                                ImageGallerySortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImageGalleries(page, pageSize, out totalItems, sortOrder);
        }

        public virtual ImageGalleryCollection GetImageGalleries(SiteCollection sites, Int32 page, Int32 pageSize,
                                                                ImageGallerySortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImageGalleries(sites, page, pageSize, sortOrder);
        }

        public virtual ImageGalleryCollection GetImageGalleries(SiteCollection sites, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                                ImageGallerySortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImageGalleries(sites, page, pageSize, out totalItems, sortOrder);
        }

        public virtual Int32 GetNumOfImages(PublishState state)
        {
            return ImageGalleryHandler.GetNumOfImages(state);
        }

        public virtual Int32 GetNumOfImages(PublishState state, Boolean ignoreCache)
        {
            return ImageGalleryHandler.GetNumOfImages(state, ignoreCache);
        }

        public virtual Int32 GetNumOfImages(PublishState state, SiteCollection sites, Boolean ignoreCache)
        {
            return ImageGalleryHandler.GetNumOfImages(state, sites, ignoreCache);
        }

        public virtual Int32 GetNumOfImages(IUser uploader)
        {
            return ImageGalleryHandler.GetNumOfImages(uploader);
        }

        public virtual void ChangeUploader(EPiServer.Community.ImageGallery.ImageGallery gallery, IUser oldUploader, IUser newUploader)
        {
            ImageGalleryHandler.ChangeUploader(gallery, oldUploader, newUploader);
        }

        public virtual Image AddImage(Image image, Boolean ignoreQuota)
        {
            return ImageGalleryHandler.AddImage(image, ignoreQuota);
        }

        public virtual void UpdateImage(Image image)
        {
            ImageGalleryHandler.UpdateImage(image);
        }

        public virtual void RemoveImage(Image image)
        {
            ImageGalleryHandler.RemoveImage(image);
        }

        public virtual void ImportEditedImage(Image image, String source, ImageAction[] actions)
        {
            ImageGalleryHandler.ImportEditedImage(image, source, actions);
        }

        public virtual Image AddImage(Image image)
        {
            return ImageGalleryHandler.AddImage(image);
        }

        public virtual Image GetImage(Int32 id)
        {
            return ImageGalleryHandler.GetImage(id);
        }

        public virtual ImageCollection GetImages(Int32 page, Int32 pageSize, ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(page, pageSize, sortOrder);
        }

        public virtual ImageCollection GetImages(Int32 page, Int32 pageSize, out Int32 totalItems, ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(page, pageSize, out totalItems, sortOrder);
        }

        public virtual ImageCollection GetImages(Int32 page, Int32 pageSize, PublishState publishState, ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(page, pageSize, publishState, sortOrder);
        }

        public virtual ImageCollection GetImages(Int32 page, Int32 pageSize, PublishState publishState, out Int32 totalItems,
                                                 ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(page, pageSize, publishState, out totalItems, sortOrder);
        }

        public virtual ImageCollection GetImages(Int32 page, Int32 pageSize, PublishState publishState, Boolean ignoreCache,
                                                 ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(page, pageSize, publishState, ignoreCache, sortOrder);
        }

        public virtual ImageCollection GetImages(Int32 page, Int32 pageSize, PublishState publishState, Boolean ignoreCache,
                                                 out Int32 totalItems, ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(page, pageSize, publishState, ignoreCache, out totalItems, sortOrder);
        }

        public virtual ImageCollection GetImages(ImageGalleryCollection galleries, Int32 page, Int32 pageSize, PublishState publishState,
                                                 ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(galleries, page, pageSize, publishState, sortOrder);
        }

        public virtual ImageCollection GetImages(ImageGalleryCollection galleries, Int32 page, Int32 pageSize, PublishState publishState,
                                                 out Int32 totalItems, ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(galleries, page, pageSize, publishState, out totalItems, sortOrder);
        }

        public virtual ImageCollection GetImages(ImageGalleryCollection galleries, Int32 page, Int32 pageSize, PublishState publishState,
                                                 Boolean ignoreCache, ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(galleries, page, pageSize, publishState, ignoreCache, sortOrder);
        }

        public virtual ImageCollection GetImages(ImageGalleryCollection galleries, Int32 page, Int32 pageSize, PublishState publishState,
                                                 Boolean ignoreCache, out Int32 totalItems, ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(galleries, page, pageSize, publishState, ignoreCache, out totalItems, sortOrder);
        }

        public virtual ImageCollection GetImages(ImageGalleryCollection galleries, Int32 page, Int32 pageSize, PublishState publishState,
                                                 Boolean ignoreCache, TimeSpan? explicitCacheTime, out Int32 totalItems,
                                                 ImageSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImages(galleries, page, pageSize, publishState, ignoreCache, explicitCacheTime, out totalItems,
                                                 sortOrder);
        }

        public virtual ImageComment AddImageComment(ImageComment comment)
        {
            return ImageGalleryHandler.AddImageComment(comment);
        }

        public virtual void UpdateImageComment(ImageComment comment)
        {
            ImageGalleryHandler.UpdateImageComment(comment);
        }

        public virtual void RemoveImageComment(ImageComment comment)
        {
            ImageGalleryHandler.RemoveImageComment(comment);
        }

        public virtual ImageComment GetImageComment(Int32 id)
        {
            return ImageGalleryHandler.GetImageComment(id);
        }

        public virtual ImageCommentCollection GetImageComments(Image image, Int32 page, Int32 pageSize, ImageCommentSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImageComments(image, page, pageSize, sortOrder);
        }

        public virtual ImageCommentCollection GetImageComments(Image image, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                               ImageCommentSortOrder[] sortOrder)
        {
            return ImageGalleryHandler.GetImageComments(image, page, pageSize, out totalItems, sortOrder);
        }

        public virtual void RemoveThumbnails(Image image)
        {
            ImageGalleryHandler.RemoveThumbnails(image);
        }

        public virtual Thumbnail GetThumbnail(Image image, Int32 width, Int32 height, ThumbnailFormat thumbnailFormat)
        {
            return ImageGalleryHandler.GetThumbnail(image, width, height, thumbnailFormat);
        }

        public virtual Thumbnail GetThumbnail(Image image, Int32 width, Int32 height, ThumbnailFormat thumbnailFormat,
                                              Boolean createThumbnail)
        {
            return ImageGalleryHandler.GetThumbnail(image, width, height, thumbnailFormat, createThumbnail);
        }

        public virtual Thumbnail GetThumbnail(Image image, Int32 width, Int32 height, ThumbnailFormat thumbnailFormat,
                                              Boolean createThumbnail, Boolean tagThumbnail)
        {
            return ImageGalleryHandler.GetThumbnail(image, width, height, thumbnailFormat, createThumbnail, tagThumbnail);
        }

        public virtual Thumbnail GetThumbnail(Image image, Int32 width, Int32 height, ThumbnailFormat thumbnailFormat,
                                              Boolean createThumbnail, Watermark thumbnailTag)
        {
            return ImageGalleryHandler.GetThumbnail(image, width, height, thumbnailFormat, createThumbnail, thumbnailTag);
        }

        public virtual Thumbnail GetThumbnail(Image image, Int32 width, Int32 height, ThumbnailFormat thumbnailFormat,
                                              Boolean createThumbnail, Watermark thumbnailTag, Color thumbBgColor)
        {
            return ImageGalleryHandler.GetThumbnail(image, width, height, thumbnailFormat, createThumbnail, thumbnailTag, thumbBgColor);
        }

        public virtual ImageCollection GetQueryResult(ImageQuery query)
        {
            return ImageGalleryHandler.GetQueryResult(query);
        }

        public virtual ImageCollection GetQueryResult(ImageQuery query, Int32 page, Int32 pageSize)
        {
            return ImageGalleryHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ImageCollection GetQueryResult(ImageQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return ImageGalleryHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual ImageGalleryCollection GetQueryResult(ImageGalleryQuery query)
        {
            return ImageGalleryHandler.GetQueryResult(query);
        }

        public virtual ImageGalleryCollection GetQueryResult(ImageGalleryQuery query, Int32 page, Int32 pageSize)
        {
            return ImageGalleryHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ImageGalleryCollection GetQueryResult(ImageGalleryQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return ImageGalleryHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual ImageCommentCollection GetQueryResult(ImageCommentQuery query)
        {
            return ImageGalleryHandler.GetQueryResult(query);
        }

        public virtual ImageCommentCollection GetQueryResult(ImageCommentQuery query, Int32 page, Int32 pageSize)
        {
            return ImageGalleryHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ImageCommentCollection GetQueryResult(ImageCommentQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return ImageGalleryHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnImageAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageAdded != null)
                ImageAdded(sender, e);
        }

        public virtual void OnImageUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageUpdated != null)
                ImageUpdated(sender, e);
        }

        public virtual void OnImageRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageRemoved != null)
                ImageRemoved(sender, e);
        }

        public virtual void OnImageVote(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageVote != null)
                ImageVote(sender, e);
        }

        public virtual void OnImageGalleryAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageGalleryAdded != null)
                ImageGalleryAdded(sender, e);
        }

        public virtual void OnImageGalleryUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageGalleryUpdated != null)
                ImageGalleryUpdated(sender, e);
        }

        public virtual void OnImageGalleryRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageGalleryRemoved != null)
                ImageGalleryRemoved(sender, e);
        }

        public virtual void OnImageCommentAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageCommentAdded != null)
                ImageCommentAdded(sender, e);
        }

        public virtual void OnImageCommentUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageCommentUpdated != null)
                ImageCommentUpdated(sender, e);
        }

        public virtual void OnImageCommentRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ImageCommentRemoved != null)
                ImageCommentRemoved(sender, e);
        }
    }
}